#!/usr/local/bin/python3.6
# coding:utf-8
# @Time    : 2019/11/16 15:46
# @Author  : ZhangBing
# @Email   : 18829272841@163.com
# @File    : utils.py
# @Software: PyCharm
import logging
from functools import wraps
from flask import redirect, session, url_for


def getlogger(mod_name, filepath: str = '/var/log/flaskapp'):
    logger = logging.getLogger(mod_name)
    logger.setLevel(logging.INFO)
    handler = logging.FileHandler("{}/{}.log".format(filepath, mod_name))  # 此处获取和填写相关的配置
    fmt = logging.Formatter("%(asctime)s  %(levelname)s   modules:  %(module)s  %(message)s")
    handler.setFormatter(fmt)
    logger.addHandler(handler)
    return logger


'''
此处用于判断用户是否登陆过，可通过对用户的session的获取来完成此种验证操作
'''


def login_auth(fn):
    @wraps(fn)
    def __wapper(*args, **kwargs):
        if session.get('id'):
            return fn(*args, **kwargs)
        else:
            return redirect(url_for('user.login'))

    return __wapper


if __name__ == "__main__":
    logg = getlogger('test')
    logg.info('1234354')
